package com.pcg.posgasstation.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.pcg.posgasstation.R;
import com.pcg.posgasstation.interfaces.DialogCallback;
import com.pcg.posgasstation.model.Constant;

public class DialogHelper {

    AlertDialog mCommonDialog = null;

    private static DialogHelper sDialogHelper = new DialogHelper();

    public static DialogHelper getInstance() {
        return sDialogHelper;
    }

    public Dialog showPayPasswordDialog(final Context context, final DialogCallback dialogCallback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View content = LayoutInflater.from(context).inflate(R.layout.layout_pay_password_dialog, null);

        TextView tvCancel = content.findViewById(R.id.tvDialogCancel);
        TextView tvConfirm = content.findViewById(R.id.tvDialogConfirm);
        final EditText edtPassword = content.findViewById(R.id.edtPassword);

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mCommonDialog.dismiss();
                dialogCallback.onCancel();
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mCommonDialog.dismiss();
                dialogCallback.onConfirm(edtPassword.getText().toString());
            }
        });
        builder.setCancelable(false);
        builder.setView(content);
        mCommonDialog = builder.create();
        mCommonDialog.setCanceledOnTouchOutside(false);

        mCommonDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                LogUtil.e("DialogHelper", "mCommonDialog onShow");
                KeyboardUtil.showSoftInput(context, edtPassword);
//                KeyboardUtil.toggleSoftInput(context);
            }
        });

        mCommonDialog.show();

        return mCommonDialog;
    }

    public Dialog showGenderDialog(Context context, int currentGender,
                                   final View.OnClickListener boyListener,
                                   final View.OnClickListener girlListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View content = LayoutInflater.from(context).inflate(R.layout.layout_gender_dialog, null);
        TextView tvGenderBoy = content.findViewById(R.id.tvDialogGenderBoy);
        TextView tvGenderGirl = content.findViewById(R.id.tvDialogGenderGirl);

        if (currentGender == Constant.Gender.GENDER_BOY) {
            tvGenderBoy.setTypeface(Typeface.DEFAULT_BOLD);
            tvGenderGirl.setTypeface(Typeface.DEFAULT);
        } else if (currentGender == Constant.Gender.GENDER_GIRL) {
            tvGenderBoy.setTypeface(Typeface.DEFAULT);
            tvGenderGirl.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            tvGenderBoy.setTypeface(Typeface.DEFAULT);
            tvGenderGirl.setTypeface(Typeface.DEFAULT);
        }

        tvGenderBoy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boyListener.onClick(v);
                mCommonDialog.dismiss();
            }
        });

        tvGenderGirl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                girlListener.onClick(v);
                mCommonDialog.dismiss();
            }
        });

        builder.setCancelable(false);
        builder.setView(content);
        mCommonDialog = builder.create();
        mCommonDialog.setCanceledOnTouchOutside(false);
        mCommonDialog.show();

        return mCommonDialog;
    }

}
